package com.example.administrator.managesystem.view;

import android.os.Parcel;
import android.os.Parcelable;
import android.view.View;


import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.adapter.StringAdapter;
import com.example.administrator.managesystem.ui.BasePickerView;
import com.example.administrator.managesystem.ui.PickerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 51425 on 2019/9/24.
 */

public class WheelDialog extends BaseBottomDialog {

    private List<String> mList;
    private PickerView mPickerView;

    public static WheelDialog initDefault(List<String> mKindList, int size, MyDialogFragment.ItemClickListener itemClickListener) {
        WheelDialog dialog = (WheelDialog) new Builder().setList(mKindList).setSize(size)
                .setLayoutId(R.layout.dialog_wheel).setInterval(0).build(new WheelDialog()).setItemClickListener(itemClickListener);
        return dialog;
    }

    @Override
    protected void initData() {
        super.initData();
        WheelDialog.Builder builder = (WheelDialog.Builder) mBuilder;
        mList = builder.list;
        int size = builder.size;
        mPickerView = mParentView.findViewById(R.id.wheelView);
        // 覆盖xml中的水平方向
        mPickerView.setHorizontal(false);
        mPickerView.setTextSize(15, 22);
        mPickerView.setIsCirculation(true);
        mPickerView.setItemSize(50);
        StringAdapter stringAdapter = new StringAdapter(mList);
        mPickerView.setAdapter(stringAdapter);
        mPickerView.setOnSelectedListener(new BasePickerView.OnSelectedListener() {
            @Override
            public void onSelected(BasePickerView pickerView, int position) {
                if (itemClickListener != null)
                    itemClickListener.onClick(position);
            }
        });


        mParentView.findViewById(R.id.cancel).setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                int selectedPosition = mPickerView.getSelectedPosition();
                if (itemClickListener != null)
                    itemClickListener.onClick(selectedPosition);
                dismissDialog(getAttachActivity());
            }
        });
    }

    @Override
    protected void initListener() {
        super.initListener();

    }

    public static class Builder extends BaseBottomDialog.Builder {
        public List<String> list;
        public int size;

        public Builder() {
        }

        public List<String> getList() {
            return list;
        }

        public Builder setList(List<String> list) {
            this.list = list;
            return this;
        }
        public int getSize() {
            return size;
        }

        public Builder setSize(int size) {
            this.size = size;
            return this;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeStringList(this.list);
            dest.writeInt(this.size);
        }

        protected Builder(Parcel in) {
            super(in);
            this.list = in.createStringArrayList();
            this.size = in.readInt();
        }

        public static final Parcelable.Creator<Builder> CREATOR = new Parcelable.Creator<Builder>() {
            @Override
            public Builder createFromParcel(Parcel source) {
                return new Builder(source);
            }

            @Override
            public Builder[] newArray(int size) {
                return new Builder[size];
            }
        };
    }
}
